package engine;

import role.Player;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class Save {
    /**
     * 游戏存档方法
     */
    public void save(Player player, List<String> items) throws Exception {
        //序列化
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("player.dat"));

        //序列化对象
        oos.writeObject(player);

        oos = new ObjectOutputStream(new FileOutputStream("items.dat"));
        oos.writeObject(items);

        oos.flush();
        oos.close();
    }

    /**
     * 游戏读档方法
     */
    public List<Object> read() throws Exception {
        List<Object> list = new ArrayList<>();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("player.dat"));
        Object obj = ois.readObject();
        //System.out.println(obj);
        Player p  = (Player) obj;
        list.add(p);
        //System.out.println(p);


        ois = new ObjectInputStream(new FileInputStream("items.dat"));
        Object obj1 = ois.readObject();
        //System.out.println(obj);
        ArrayList<String> item  = (ArrayList<String>) obj1;
        list.add(item);


        ois.close();

        return list;
    }


}
